package com.taxshare.seller.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.constant.ResultConstant;
import com.taxshare.seller.entity.CarCategoryEntity;
import com.taxshare.seller.entity.GoodsManageEntity;
import com.taxshare.seller.mapper.CarCategoryMapper;
import com.taxshare.seller.view.dto.CarCategoryDTO;
import com.taxshare.seller.view.dto.GoodsManageDTO;
import com.taxshare.seller.view.dto.TaxComplilationDTO;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.UUIDUitl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author cxl
 * @date 2021/8/19 11:59
 */
@Service
public class CarCategoryService extends ServiceImpl<CarCategoryMapper, CarCategoryEntity> {
    /**
     * 车辆种类【车辆种类信息新增】
     *
     * @param carCategoryDTO 新增参数
     * @return 结果
     */
    public Response addCarCategory(CarCategoryDTO carCategoryDTO) {
        if (CollectionUtil.isNotEmpty(listArCategoryByName(carCategoryDTO.getCarCategoryName()))) {
            return Response.error("SEL_CAR_003");
        }
        CarCategoryEntity carCategoryEntity = new CarCategoryEntity();
        BeanUtils.copyProperties(carCategoryDTO, carCategoryEntity);
        carCategoryEntity.setCreateUserid(UserInfoUtil.getUserID());
        carCategoryEntity.setCreateTime(new Date());
        carCategoryEntity.setUuid(UUIDUitl.UUID32(false));
        this.save(carCategoryEntity);
        return Response.success();
    }

    /**
     * 车辆种类【车辆种类信息删除】
     *
     * @param ids 主键ids
     * @return 结果
     */
    public Response removeCarCategory(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.error("SEL_CAR_001");
        }
        this.removeByIds(ids);
        return Response.success();
    }

    public List<CarCategoryEntity> listArCategoryByName(String name) {
        QueryWrapper<CarCategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("car_category_name", name);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 车辆种类【车辆种类信息编辑】
     *
     * @param carCategoryDTO 参数
     * @return 结果
     */
    public Response editCarCategory(CarCategoryDTO carCategoryDTO) {
        if(StringUtils.isEmpty(carCategoryDTO.getUuid())){
            return Response.error("SEL_CAR_001");
        }
        CarCategoryEntity byId = this.getById(carCategoryDTO.getUuid());
        List<CarCategoryEntity> carCategoryEntities = listArCategoryByName(carCategoryDTO.getCarCategoryName());
        if (!StringUtils.equals(byId.getCarCategoryName(), carCategoryDTO.getCarCategoryName())
                && !CollectionUtils.isEmpty(carCategoryEntities)) {
            return Response.error("SEL_CAR_003");
        }
        CarCategoryEntity carCategoryEntity = new CarCategoryEntity();
        BeanUtils.copyProperties(carCategoryDTO, carCategoryEntity);
        carCategoryEntity.setUpdateUserid(UserInfoUtil.getUserID());
        carCategoryEntity.setUpdateTime(new Date());
        this.updateById(carCategoryEntity);
        return Response.success();
    }

    /**
     * 车辆种类【车辆种类信息编辑查询】
     *
     * @param keyword 关键词
     * @return 列表
     */
    public Response listCarCategoryByKeyword(String keyword) {
        QueryWrapper<CarCategoryEntity> queryWrapper = new QueryWrapper();
        if (StrUtil.isNotEmpty(keyword)) {
            queryWrapper.and(wrapper -> wrapper.like("car_category_code", keyword)
                    .or().like("car_genera_name", keyword).or().like("car_category_name", keyword));
        }
        queryWrapper.orderByDesc("create_time");
        return Response.success(this.list(queryWrapper));
    }
}